package com.bjpowernode.test08;

import java.util.Arrays;

public class BuyCar {

    //定义方法，向购物车中添加商品，添加的商品放到队尾
    public Product[] addPro(Product[] products,Product pro){
        Product[] newProducts = new Product[products.length+1];
        for (int i=0;i<newProducts.length;i++){
            if (i == newProducts.length-1){
                newProducts[i] = pro;
            }else {
                newProducts[i] = products[i];
            }
        }
        return newProducts;
    }

    //定义方法，展示购物车中的商品和总价格
    public void printPro(Product[] products){
        for (int i=0;i<products.length;i++){
            System.out.println(products[i].getId()+" "+products[i].getName()+" "+products[i].getPrice());
        }
    }

    //定义方法，删除购物车中的商品
    //此方法存在缺陷，如果购物车中存在两个完全一样的商品，则会导致异常
    public Product[] delPro(Product[] products,Product pro){
        Product[] newProducts = new Product[products.length-1];
        int index = 0;
        for (int i=0;i<products.length;i++){
            if (products[i] != pro){
                newProducts[index++] = products[i];
            }
        }
        return newProducts;
    }

}
